package com.dk.controller;

import com.alibaba.druid.support.spring.stat.SpringStatUtils;
import com.dk.Vo.TUserCardVo;
import com.dk.pojo.TCity;
import com.dk.pojo.TUserCard;
import com.dk.service.TCityService;
import com.dk.service.TUserCardServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("tUserCardController")
public class TUserCardController {
    //身份
    @Autowired
    private TUserCardServiceI tUserCardServiceI;

    @Autowired
    private TCityService tCityService;

    //跳转查询页面
    @RequestMapping("list")
    public String listGet(){
        return "card/list";
    }

    //查询身份
    @RequestMapping("getInfoServer")
    @ResponseBody
    public List<TUserCardVo> getInfoServer(){
        List<TUserCardVo> list =  tUserCardServiceI.getInfoServer();
        return list;
    }

    //跳转增加页面
    @RequestMapping("insertInfo")
    public String insertInfo(){
        return "card/add";
    }

    //确认增加
    @RequestMapping("saveById")
    @ResponseBody
    public int saveById(TUserCard tUserCard){
        tUserCardServiceI.saveById(tUserCard);
        return 1;
    }

    //跳转修改页面
    @RequestMapping("updateInfo/{userId}")
    public String getAllfind(@PathVariable("userId") Integer userId, Model model){

        TUserCard tUserCards = tUserCardServiceI.getAllfind(userId);
        model.addAttribute("tUserCards",tUserCards);

        //获取省
        //首先根据省的pid:0 查询所有
        List<TCity> sheng = tCityService.getSheng(0);
        //获取身份的省号
        Integer province = tUserCards.getProvince();

        //获取市
        //根据省号查询所有的市pid
        List<TCity> shi = tCityService.getSheng(province);
        //获取身份的市号
        Integer city = tUserCards.getCity();

        //获取县
        //根据市号查询所有的县pid
        List<TCity> xian = tCityService.getSheng(city);
        Integer country = tUserCards.getCountry();

        model.addAttribute("sheng",sheng);
        model.addAttribute("shi",shi);
        model.addAttribute("xian",xian);
        return "card/update";
    }

    //确认修改
    @RequestMapping("updateById")
    @ResponseBody
    public  int updateById(TUserCard tUserCard){
        tUserCardServiceI.updateById(tUserCard);
        return 1;
    }

    //删除
    @RequestMapping("deleteById/{userId}")
    @ResponseBody
    public int deleteById(@PathVariable("userId") Integer userId){
        tUserCardServiceI.deleteById(userId);
        return 1;
    }
}
